﻿Imports SIRData
Imports SIREntities
Public Class frmProductoAgregar

  Private m_bSave As Boolean = False
  Private m_InventarioDetalle As New clsInventarioDetalleBE

  Public Property obSave() As Boolean
    Get
      Return m_bSave
    End Get
    Set(ByVal value As Boolean)
      m_bSave = value
    End Set
  End Property

  Public Property oInventarioDetalle() As clsInventarioDetalleBE
    Get
      Return m_InventarioDetalle
    End Get
    Set(ByVal value As clsInventarioDetalleBE)
      m_InventarioDetalle = value
    End Set
  End Property


  Private Sub ListarProductos()
    Dim m_Producto As New clsProducto(g_sConnectString)
    Try

      dgvListado.DataSource = m_Producto.ProductoListar(rbCodigo.Checked, txtvFiltro.Text.Trim)

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_Producto = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
    Try
      Call ListarProductos()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvListado.RowPrePaint
    Try
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 8)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvListado.DoubleClick
    Try
      Call SeleccionarProducto()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnSeleccionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeleccionar.Click
    Try
      Call SeleccionarProducto()

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub SeleccionarProducto()
    Dim oForm As New frmDetalleInventarioAgregar
    Try
      Me.Enabled = False

      oForm.bNuevo = True
      oForm.lblcCodProd.Text = dgvListado.Item("cCodProd", dgvListado.CurrentCell.RowIndex).Value
      oForm.lblvNomProd.Text = dgvListado.Item("vNomProd", dgvListado.CurrentCell.RowIndex).Value
      oForm.ShowDialog()

      Me.Enabled = True

      If oForm.obSave Then
        m_bSave = True
        m_InventarioDetalle.iNumInv = 0
        m_InventarioDetalle.siCodProd = dgvListado.Item("siCodProd", dgvListado.CurrentCell.RowIndex).Value
        m_InventarioDetalle.cCodProd = dgvListado.Item("cCodProd", dgvListado.CurrentCell.RowIndex).Value
        m_InventarioDetalle.vNomProd = dgvListado.Item("vNomProd", dgvListado.CurrentCell.RowIndex).Value
        m_InventarioDetalle.tiUniMed = dgvListado.Item("tiUniMed", dgvListado.CurrentCell.RowIndex).Value
        m_InventarioDetalle.vUniMed = dgvListado.Item("vUniMed", dgvListado.CurrentCell.RowIndex).Value

        m_InventarioDetalle.siCantidad = oForm.osiCantidad
        m_InventarioDetalle.vObsInv = oForm.ovObsInv
        Me.Close()
      End If

    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      GC.Collect()
    End Try
  End Sub

End Class